package client

import (
	"context"
	fitness "fitness/common/proto"
	"google.golang.org/grpc"
)

type Handler func(ctx context.Context, client fitness.FitnessClient) (interface{}, error)

func FitnessClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8082", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := fitness.NewFitnessClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}
func UserLogin(ctx context.Context, in *fitness.UserLoginReq) (*fitness.UserLoginResp, error) {
	client, err := FitnessClient(ctx, func(ctx context.Context, client fitness.FitnessClient) (interface{}, error) {
		login, err := client.UserLogin(ctx, in)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*fitness.UserLoginResp), nil
}
func AddReservation(ctx context.Context, in *fitness.AddReservationReq) (*fitness.AddReservationResp, error) {
	client, err := FitnessClient(ctx, func(ctx context.Context, client fitness.FitnessClient) (interface{}, error) {
		reservation, err := client.AddReservation(ctx, in)
		if err != nil {
			return nil, err
		}
		return reservation, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*fitness.AddReservationResp), nil
}
func FitnessList(ctx context.Context, in *fitness.FitnessListReq) (*fitness.FitnessListResp, error) {
	client, err := FitnessClient(ctx, func(ctx context.Context, client fitness.FitnessClient) (interface{}, error) {
		list, err := client.FitnessList(ctx, in)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*fitness.FitnessListResp), nil
}
func ReservationList(ctx context.Context, in *fitness.ReservationListReq) (*fitness.ReservationListResp, error) {
	client, err := FitnessClient(ctx, func(ctx context.Context, client fitness.FitnessClient) (interface{}, error) {
		list, err := client.ReservationList(ctx, in)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*fitness.ReservationListResp), nil
}
func FindUserReservation(ctx context.Context, in *fitness.FindUserReservationReq) (*fitness.FindUserReservationResp, error) {
	client, err := FitnessClient(ctx, func(ctx context.Context, client fitness.FitnessClient) (interface{}, error) {
		reservation, err := client.FindUserReservation(ctx, in)
		if err != nil {
			return nil, err
		}
		return reservation, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*fitness.FindUserReservationResp), nil
}
